package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.api.ResultCode;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.request.GtPushRequest;
import com.ytjj.qmyx.admin.model.request.UserAddRequest;
import com.ytjj.qmyx.admin.model.request.UserRequest;
import com.ytjj.qmyx.admin.model.request.UserTrueRequest;
import com.ytjj.qmyx.admin.model.response.UserInfosResponse;
import com.ytjj.qmyx.admin.service.UserInfoService;
import com.ytjj.qmyx.admin.service.UserService;
import com.ytjj.qmyx.admin.utils.UserUtil;
import com.ytjj.qmyx.users.model.UsersExpert;
import com.ytjj.qmyx.users.model.UsersInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 用户
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询所有用户
     *
     * @param user
     * @return
     */
    @PostMapping("/list")
    public CommonResult list(@RequestBody UserRequest user) {

        CommonPage<UserInfosResponse> pageList = userService.selectList(user);
        if(!UserUtil.isAdmin() && pageList !=null  && !CollectionUtils.isEmpty(pageList.getList())) {
            pageList.getList().stream().forEach(item -> {
                item.setChannel("");
            });
        }
        return CommonResult.success(pageList);
    }

    /**
     * 查询所有虚拟用户
     *
     * @param user
     * @return
     */
    @PostMapping("/virtualList")
    public CommonResult virtualList(@RequestBody UserRequest user) {
        CommonPage<UserInfosResponse> pageList = userService.virtualList(user);
        return CommonResult.success(pageList);
    }

    /**
     * 禁用/启用
     *
     * @param id
     * @return
     */
    @GetMapping("/updateUserStatus")
    @OperLog(operModule = "用户模块", operDesc = "禁用/启用用户", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateUserStatus(@RequestParam Integer id) {
        int count = userService.updateUserStatus(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 设置达人
     *
     * @param usersExpert
     * @return
     */
    @PostMapping("/addUsersExpert")
    @OperLog(operModule = "用户模块", operDesc = "设置达人", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult addUsersExpert(@RequestBody UsersExpert usersExpert) {
        int count = userService.addUsersExpert(usersExpert);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 编辑用户
     *
     * @param request
     * @return
     */
    @PostMapping("/updateUser")
    @OperLog(operModule = "用户模块", operDesc = "编辑虚拟用户", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateUser(@RequestBody UserAddRequest request) {
        userInfoService.updateUser(request);
        return CommonResult.success();
    }

    /**
     * 编辑用户积分
     * @param request
     * @return
     */
    @PostMapping("/updateUserScore")
    @OperLog(operModule = "用户模块", operDesc = "编辑用户积分", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateUserScore(@RequestBody UserAddRequest request) {
        return userInfoService.updateUserScore(request) > 0 ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * 编辑真实用户
     * 仅允许修改基本资料，禁止修改积分和钱包
     * @param request
     * @return
     */
    @PostMapping("/updateTrueUser")
    @OperLog(operModule = "用户模块", operDesc = "编辑真实用户", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateTrueUser(@RequestBody UserTrueRequest request) {
        userInfoService.updateTrueUser(request);
        return CommonResult.success();
    }

    /**
     * 增加用户
     *
     * @param request
     * @return
     */
    @PostMapping("/addUser")
    @OperLog(operModule = "用户模块", operDesc = "增加用户", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult addUser(@RequestBody UserAddRequest request) {
        int count = userService.addUser(request);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 用户推送
     *
     * @param gtPushRequest
     * @return
     */
    @PostMapping("/gtPushByUsers")
    public CommonResult gtPushByUsers(@RequestBody GtPushRequest gtPushRequest) {
        userService.gtPushByUsers(gtPushRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }

}
